package org.openinsula.vulcano.core;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Collection;

import org.junit.Test;
import org.openinsula.vulcano.core.annotation.RequiresDependencyInjection;
import org.openinsula.vulcano.core.command.Command;
import org.openinsula.vulcano.core.command.mock.MockType;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

@ContextConfiguration
public class BeanConfigurerSupportUtilsTestCase extends AbstractJUnit4SpringContextTests {

	@Autowired
	private BeanFactory beanFactory;

	@Autowired
	private MockType mockType;

	private Command<Boolean> createPlainCommand() {
		return new Command<Boolean>() {
			private static final long serialVersionUID = 1L;

			@Autowired
			private MockType _mockType;

			@Override
			public Boolean execute() {
				return _mockType == mockType;
			}

		};
	}

	private void verify(final Command<Boolean> command) {
		assertFalse(command.execute());
		BeanConfigurerSupportUtils.configureBean(beanFactory, command);
		assertTrue(command.execute());
	}

	@Test
	public void testPlainCommand() {
		Command<Boolean> command = createPlainCommand();
		verify(command);
	}

	@Test
	public void testDecoratedCommand() {

		Command<Boolean> command = new Command<Boolean>() {
			private static final long serialVersionUID = 1L;

			@Autowired
			private MockType _mockType;

			@RequiresDependencyInjection
			private Command<Boolean> decoratedCommand = createPlainCommand();

			@Override
			public Boolean execute() {
				return _mockType == mockType && decoratedCommand.execute();
			}

		};

		verify(command);
	}

	@SuppressWarnings("unchecked")
	@Test
	public void testCompositeCommandWithArray() {

		Command<Boolean> command = new Command<Boolean>() {
			private static final long serialVersionUID = 1L;

			@Autowired
			private MockType _mockType;

			@RequiresDependencyInjection
			private Command<Boolean>[] decoratedCommand = new Command[] {
					createPlainCommand(),
					createPlainCommand()
			};

			@Override
			public Boolean execute() {
				boolean commandsResult = true;

				for (Command<Boolean> command : decoratedCommand) {
					commandsResult = commandsResult && command.execute();
				}

				return _mockType == mockType && commandsResult;
			}

		};

		verify(command);
	}

	@SuppressWarnings("unchecked")
	@Test
	public void testCompositeCommandWithCollection() {
		final Collection<Command<Boolean>> commandList = new ArrayList<Command<Boolean>>();
		commandList.add(createPlainCommand());
		commandList.add(createPlainCommand());

		Command<Boolean> command = new Command<Boolean>() {
			private static final long serialVersionUID = 1L;

			@Autowired
			private MockType _mockType;

			@RequiresDependencyInjection
			private Collection<Command<Boolean>> _commandList = commandList;

			@Override
			public Boolean execute() {
				boolean commandsResult = true;

				for (Command<Boolean> command : _commandList) {
					commandsResult = commandsResult && command.execute();
				}

				return _mockType == mockType && commandsResult;
			}

		};

		verify(command);
	}

}
